<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>student</title>
    <link rel="stylesheet" href="/bootstrap.min.css">
    <style>
        #l1, #name1, #l2, #no1, #bt {
            display: inline-block;
            width: auto;
            margin-right: 20px;
        }

        #search {
            margin: 40px 0;
        }

        #table {
            margin-top: 20px;
        }

        a {
            text-decoration: none;
            color: #ea2d62;
        }

    </style>
    <script src="/jquery.min.js"></script>
</head>
<body>
<div class="container">
    <form class="form-inline" id="search">
        <label class="sr-only" for="name1" id="l1">姓名</label>
        <input type="text" class="form-control" id="name1" name="name" placeholder="name">
        <label class="sr-only" for="no1" id="l2">学号</label>
        <input type="password" class="form-control" id="no1" placeholder="no" name="no">
        <button type="button" class="btn btn-primary" id="bt" onclick="findByName();">搜索</button>
    </form>
    <!--模态框-->
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal"
            onclick="preAdd()">
        新增学生信息
    </button>
    <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h1 class="modal-title fs-5" id="exampleModalLabel">学生信息</h1>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form id="StudentInfor">
                        <input hidden="hidden" id="id" name="id"/>
                        <div class="form-group">
                            <label for="sno">学号</label>
                            <input type="text" class="form-control" id="sno" name="sno" placeholder="sno">
                        </div>
                        <div class="form-group">
                            <label for="name">姓名</label>
                            <input type="text" class="form-control" id="name" name="name" placeholder="name">
                        </div>
                        <div class="form-group">
                            <label for="sex">性别：</label>
                            <select class="form-control" id="sex" name="sex">
                                <option value="1">男</option>
                                <option value="2">女</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="score">成绩</label>
                            <input type="text" class="form-control" id="score" name="score" placeholder="name">
                        </div>
                        <div class="form-group">
                            <label for="age">年龄</label>
                            <input type="text" class="form-control" id="age" name="age" placeholder="age">
                        </div>

                        <div class="form-group">
                            <label for="password">密码</label>
                            <input type="password" class="form-control" id="password" name="password"
                                   placeholder="password">
                        </div>

                    </form>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" onclick="saveStudent();" data-dismiss="modal">保存
                    </button>

                </div>
            </div>
        </div>
    </div>

    <table id="table" class="table table-bordered table-hover table-condensed" style="text-align: center;">
        <thead>
        <tr style="border: solid 1px black ; font-size: 17px;">
            <th>#</th>
            <th>姓名</th>
            <th>学号</th>
            <th>成绩</th>
            <th>年龄</th>
            <th>性别</th>
            <th style="width: 100px;">处理</th>
        </tr>
        </thead>
        <tbody id="tb">

        </tbody>

    </table>
</div>



<!--发送请求返回数据-->
<script src="/bootstrap.min.js"></script>
<script>
    function preAdd() {
        $("#id").val(0)
    }
    // 根据姓名模糊查询
    function findByName() {
        var name = $("#name1").val().trim()
        $.ajax({
            url: "/webapi/student/list/"+`${name}`,
            method: "get",
        }).done(function (rs) {
            var len = rs.length
            var html = ""
            for (let i = 0; i < len; i++) {
                let item = rs[i]
                html += "<tr>" +
                    "<td>" + item.id + "</td>"
                    + "<td>" + item.name + "</td>"
                    + "<td>" + item.sno + "</td>"
                    + "<td>" + item.score + "</td>"
                    + "<td>" + item.age + "</td>"
                    + "<td>" + item.sex + "</td>"
                    + "<td><a href='#' onclick='editStudent(" + item.id + ");'>编辑</a> <a href='#' onclick='deleteStudent(" + item.id + ");'>删除</a></td>"
                    + "<\tr>"
            }
            $("#tb").html(html)
            $("#name1").val(" ")
        })
    }
    // 获取学生全部信息
    function getStudentList() {
        $.ajax({
            url: "/webapi/student/list",
            method: "get",
        }).done(function (rs) {
            var len = rs.length
            var html = ""
            for (let i = 0; i < len; i++) {
                let item = rs[i]
                html += "<tr>" +
                    "<td>" + item.id + "</td>"
                    + "<td>" + item.name + "</td>"
                    + "<td>" + item.sno + "</td>"
                    + "<td>" + item.score + "</td>"
                    + "<td>" + item.age + "</td>"
                    + "<td>" + item.sex + "</td>"
                    + "<td><a href='#' onclick='editStudent(" + item.id + ");'>编辑</a> <a href='#' onclick='deleteStudent(" + item.id + ");'>删除</a></td>"
                    + "<\tr>"
            }
            $("#tb").html(html)
        })
    }

    // 新增或者更新
    function saveStudent() {
        var data = $("#StudentInfor").serialize()

        var id = $("#id").val()
        // 新增
        if (id < 1) {
            $.ajax({
                url: "/webapi/student/insert",
                method: "post",
                data: data
            }).done(function () {
                getStudentList()
                setTimeout(function () {
                        $("#exampleModal").modal("hide");
                    },
                    200);
            })
        } else {
            // 更新
            $.ajax({
                url: "/webapi/student/update",
                method: "put",
                data: data
            }).done(function () {
                getStudentList()
                setTimeout(function () {
                        $("#exampleModal").modal("hide");
                    },
                    200);
            })
        }


    }
    // 编辑学生信息
    function editStudent(id) {
        $("#exampleModal").modal("show");
        $.ajax({
            url: "/webapi/student/get/" + id
        }).done(function (res) {
            $("#id").val(res.id)
            $("#name").val(res.name)
            $("#sno").val(res.sno)
            $("#sex").val(res.sex)
            $("#score").val(res.score)
            $("#password").val("")
        })
    }
    // 删除学生信息
    function deleteStudent(id) {
        if (confirm("你确定要删除吗？")) {
            $.ajax({
                url: "/webapi/student/delete/" + id,
                method: "delete"
            }).done(function () {
                getStudentList()
            })

        }
    }

    $(function () {
        getStudentList()
    })

</script>

</body>

</html>